package com.zjy.controller;

import com.zjy.bean.GoodsCategory;
import com.zjy.dto.ApiResult;
import com.zjy.service.GoodsCategoryService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/11.
 */
@CrossOrigin
@Controller
@RequestMapping(value = "/goodsCategory")
public class GoodsCategoryController {
    @Resource
    private GoodsCategoryService goodsCategoryService;

    /**
     * 增加商品分类
     * @param goodsCategory
     * @return
     */
    @RequestMapping(value = "/addGoodsCategory",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoodsCategory(@RequestBody GoodsCategory goodsCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsCategory) || ObjectUtils.isNullOrEmpty(goodsCategory.getType())){
            result.error(CLIENT_ERROR,"缺少参数：type");
            return result;
        }else{
            //判断传入的type是否已存在，若已存在则提示换一个type值
            int flag = goodsCategoryService.checkExistType(goodsCategory.getType());
            if(flag == 1){
                result.error(CLIENT_ERROR,"type值已存在");
                return result;
            }else{
                try{
                    goodsCategoryService.addGoodsCategory(goodsCategory);
                    result.setMsg("商品分类插入成功！");
                }catch (Exception e){
                    result.error(SERVER_ERROR,e.getMessage());
                    return result;
                }
            }
        }
        return result;
    }

    /**
     * 根据Id软删除商品分类
     * @param goodsCategory
     * @return
     */
    @RequestMapping(value = "/deleteGoodsCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteGoodsCategoryById(@RequestBody GoodsCategory goodsCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsCategory) || ObjectUtils.isNullOrEmpty(goodsCategory.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String categoryId = goodsCategory.getId();
                int count = goodsCategoryService.deleteGoodsCategoryById(categoryId);
                if(count == 1){
                    result.setMsg("商品分类删除成功！");
                }else if(count == 0){
                    result.setMsg("找不到此id对应的分类，删除失败！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id更新商品分类
     * @param goodsCategory
     * @return
     */
    @RequestMapping(value = "/updateGoodsCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateGoodsCategoryById(@RequestBody GoodsCategory goodsCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsCategory) || ObjectUtils.isNullOrEmpty(goodsCategory.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                int count = goodsCategoryService.updateGoodsCategoryById(goodsCategory);
                if(count == 0){
                    result.error(SERVER_ERROR,"商品分类更新失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("商品分类更新成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id查找对应的商品分类
     * @param goodsCategory
     * @return
     */
    @RequestMapping(value = "/selectGoodsCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectGoodsCategoryById(@RequestBody GoodsCategory goodsCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsCategory) || ObjectUtils.isNullOrEmpty(goodsCategory.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String id = goodsCategory.getId();
                GoodsCategory goodsCategory1 = goodsCategoryService.selectGoodsCategoryById(id);
                if(ObjectUtils.isNullOrEmpty(goodsCategory1)){
                    result.setMsg("找不到此Id对应的商品分类");
                }else {
                    result.setData(goodsCategory1);
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 查找所有的商品分类
     * @return
     */
    @RequestMapping(value = "/selectAllGoodsCategory",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAllGoodsCategory(){
        ApiResult result = new ApiResult();
        try{
            List<GoodsCategory> list = goodsCategoryService.selectAllGoodsCategory();
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到商品分类！");
            }else {
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }
}
